package com.lanou.sinasports.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.sinasports.R;
import com.lanou.sinasports.beans.MyTeam;
import com.lanou.sinasports.utils.DoubleCache;

import java.util.List;


/**
 * Created by dllo on 16/1/19.
 */
public class MyTeamAdapter extends BaseAdapter {
    private Context context;
    private List<MyTeam> data;
    private RequestQueue queue;
    private ImageLoader imageLoader;

    public MyTeamAdapter(Context context, List<MyTeam> data) {
        this.context = context;
        this.data = data;
        queue= Volley.newRequestQueue(context);
        imageLoader=new ImageLoader(queue,new DoubleCache());
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyTeamViewHolder holder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_myteam,parent,false);
            holder=new MyTeamViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (MyTeamViewHolder) convertView.getTag();
        }

        holder.tvName.setText(data.get(position).getName());
        holder.tvTime.setText(data.get(position).getTime());
        holder.tvDate.setText(data.get(position).getDate());

        holder.ivMyTeam.setDefaultImageResId(R.mipmap.ico_wc_title_back_p);
        holder.ivMyTeam.setErrorImageResId(R.mipmap.ic_main_tab_news_p);
        holder.ivMyTeam.setImageUrl(data.get(position).getFlag1(), imageLoader);

        holder.ivTeam.setDefaultImageResId(R.mipmap.ico_wc_title_back_p);
        holder.ivTeam.setErrorImageResId(R.mipmap.ic_main_tab_news_p);
        holder.ivTeam.setImageUrl(data.get(position).getFlag2(),imageLoader);

        return convertView;
    }

    class MyTeamViewHolder{
        TextView tvName;
        TextView tvTime;
        TextView tvDate;
        NetworkImageView ivMyTeam;
        NetworkImageView ivTeam;

        public MyTeamViewHolder(View view) {
            tvName= (TextView) view.findViewById(R.id.iv_my_team_name);
            tvTime= (TextView) view.findViewById(R.id.tv_my_team_time);
            tvDate= (TextView) view.findViewById(R.id.tv_my_team_date);
            ivMyTeam= (NetworkImageView) view.findViewById(R.id.iv_my_team_main_team);
            ivTeam= (NetworkImageView) view.findViewById(R.id.iv_my_team_name_two);
        }
    }
}
